/*
 * @Author: c
 * @Date: 2024-01-09 12:24:59
 * @LastEditTime: 2024-04-02 18:14:54
 * @LastEditors: c
 * @Description: 
 * @FilePath: \music-web-backend\src\core\music\song\entities\song.entity.ts
 * https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: c
 * @Date: 2024-01-09 12:24:59
 * @LastEditTime: 2024-01-13 03:01:20
 * @LastEditors: c
 * @Description: 
 * @FilePath: \music-web-backend\src\core\music\song\entities\song.entity.ts
 * https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/**
 * 歌曲类型
 */

import { BasicEntity } from "src/common/entity/basic.entity";
import { LangEnum } from "src/common/enums";
import { RateFileInfo } from "src/core/music/rate_file_info/entity/audio.enity";
import { FileInfo } from "src/core/system/minio/entities/file_info.entity";
import { User } from "src/core/system/user/entities/user.entity";
import { BeforeInsert, BeforeUpdate, Column, Entity, JoinColumn, JoinTable, ManyToMany, ManyToOne, OneToMany, OneToOne, PrimaryGeneratedColumn } from "typeorm";
import { Album } from "../../album/entities/album.entity";
import { Artist } from "../../artist/entities/artist.entity";
import { Category } from "../../category/entities/category.entity";
import { Track } from "../../track/entities/track.entity";
import { Video } from "../../video/entities/video.entity";

@Entity('song')
export class Song extends BasicEntity {

    // //歌曲唯一id（"T10052891371"）
    // id: string;

    @PrimaryGeneratedColumn()
    id: number;

    //歌曲唯一code（"T10052891371"）
    @Column({
        name: "asset_id",
        type: "varchar",
        nullable: true,
        default: '',
        comment: '歌曲唯一code'
    })
    assetId: string;


    @Column({
        name: "introduce",
        type: 'varchar',
        length: 500,
        default: '',
        comment: '介绍'
    })
    introduce: string;

    //歌曲标题
    @Column({
        name: "title",
        type: 'varchar',
        length: 100,
        comment: '歌曲标题'
    })
    title: string;


    //是否是VIP歌曲
    @Column({
        name: "isVip",
        type: 'boolean',
        comment: '是否是vip歌曲'
    })
    isVip: boolean;

    //语言（中文）
    @Column({
        type: 'enum',
        enum: LangEnum,
        nullable: true,
        default: LangEnum.CN,
        comment: '语言（中文）'
    })
    lang: LangEnum;


    //发行时间
    @Column({
        type: 'date',
        name: "release_date",
        nullable: true,
        comment: "发行时间"
    })
    releaseDate: Date;

    //排序
    @Column({
        type: 'int',
        name: "sort",
        nullable: false,
        default: 1,
        comment: "排序"
    })
    sort: number;

    //最小音量
    @Column({
        type: 'int',
        name: "mean_volume",
        nullable: true,
        default: 50,
        comment: "最小音量"
    })
    meanVolume: number;

    //最大音量
    @Column({
        type: 'int',
        name: "max_volume",
        nullable: true,
        default: 100,
        comment: "最大音量"
    })
    maxVolume: number;


    //歌词文本
    @Column({
        type: 'longtext',
        name: "lyric",
        nullable: true,
        default: null,
        comment: "歌词"
    })
    lyric: string;
    //歌词链接(https://img01.dmhmusic.com/0208/M00/FD/50/ChR47F3bpFyAX5VYABs_kRz7EgM751.jpg)
    @Column({
        type: 'varchar',
        name: "lyric_url",
        length: 500,
        nullable: true,
        default: null,
        comment: "歌词链接"
    })
    lyricUrl: string;

    //歌曲唯一code（"T10052891371"）
    @Column({
        type: 'varchar',
        name: "TSID",
        nullable: true,
        default: null,
        comment: "歌曲唯一code"
    })
    TSID: string;

    //歌曲所有的播放质量类型
    allRate: string[];

    //上架时间
    @Column({
        type: 'date',
        name: "push_time",
        nullable: true,
        comment: "上架时间"
    })
    pushTime: Date;

    //下架时间
    @Column({
        type: 'date',
        name: "down_time",
        nullable: true,
        comment: "下架时间"
    })
    downTime: string;

    //2:免费 3:收费
    @Column({
        type: 'int',
        name: "pay_model",
        default: 2,
        comment: "2:免费 3:收费"
    })
    payModel: number;

    @Column({
        type: 'varchar',
        name: "album_title",
        comment: "专辑名称"
    })
    albumTitle: string;

    @Column({
        name: "artist_name",
        type: "simple-array",
        comment: "歌手名称"
    })
    artistName: string[]


    /**
  * 使用装饰器@BeforeInsert来装饰encryptPwd方法，表示该方法在数据插入之前调用，这样就能保证插入数据库的密码都是加密后的。
  */
    // @BeforeUpdate()
    // async beforeUpdate() {
    //     this.artistName = this.artistList.map(artist => artist.name)
    //     this.albumTitle = this.album.title
    //     debugger
    // }
    @BeforeUpdate()
    @BeforeInsert()
    async AddOrModify() {
        // this.artistName = this.artistList.map(artist => artist.name)
        // this.albumTitle = this.album.title
        
    }

    //歌手列表
    @ManyToMany(() => Artist, artist => artist.songList, {
        cascade: true,
        onDelete: 'CASCADE' //级联删除关联的artist
    })
    artistList: Artist[];

    //类别下面的子类别(摇滚-朋克)
    @ManyToMany(() => Category, cate => cate.songList, {
        cascade: true,
    })
    @JoinTable({
        name: "song_cate", //生成的中间表名
        joinColumn: {
            name: "song_id", //生成的列名
            referencedColumnName: "id" //需要关联的列
        },
        inverseJoinColumn: {
            name: "cate_id", //需要被关联的生成的列名
            referencedColumnName: "id"//需要关联的列
        }
    })
    genre: Category[];

    //喜欢此歌曲的人
    @ManyToMany(() => User, user => user.favorSongList, {
        cascade: true,
    })
    @JoinTable({
        name: "song_user_favor", //生成的中间表名
        joinColumn: {
            name: "song_id", //生成的列名
            referencedColumnName: "id" //需要关联的列
        },
        inverseJoinColumn: {
            name: "user_id", //需要被关联的生成的列名
            referencedColumnName: "id"//需要关联的列
        }
    })
    favoriteUsers: User[];
    //购买此歌曲的人
    @ManyToMany(() => User, user => user.paidSongList, {
        cascade: true,
    })
    @JoinTable({
        name: "song_user_paid", //生成的中间表名
        joinColumn: {
            name: "song_id", //生成的列名
            referencedColumnName: "id" //需要关联的列
        },
        inverseJoinColumn: {
            name: "user_id", //需要被关联的生成的列名
            referencedColumnName: "id"//需要关联的列
        }
    })
    paidUsers: User[];


    //被加入的歌单列表
    @ManyToMany(() => Track, track => track.songList)
    trackList: Track[];

    //歌曲MV
    @OneToOne(() => Video, video => video.song, {
        cascade: true,
        // onDelete:'CASCADE'
    })
    @JoinColumn({
        name: "video_id", //更改列名称 默认：Entity名字+Id
        // referencedColumnName:'xx' //关联其它的列表名称
    })
    video: Video;

    //歌曲质量列表
    @OneToMany(() => RateFileInfo, (rateFileInfo) => rateFileInfo.song, {
        cascade: true,
    })
    rateFileInfos: RateFileInfo[]

    //封面图
    @OneToMany(() => FileInfo, (picture) => picture.song, {
        cascade: true,
        // onDelete: 'CASCADE',
    })
    pic: FileInfo[]

    /**
     * 歌曲关联的专辑
     */
    @ManyToOne(() => Album, album => album.trackList,
        {
            onDelete: 'CASCADE', //根据albumId外键级联删除歌曲
        }
    )
    @JoinColumn({
        name: 'album_id',
        referencedColumnName: "id",
    })
    album: Album;


    constructor({ id } = {} as any) {
        super()
        this.id = id
    }

}

